package com.example.javaee_library.dao;

import com.example.javaee_library.Entity.Book;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Service;

import java.util.List;
@Mapper
public interface BookDao {

	/**
	 * 通过ID查询单本图书
	 * 
	 * @param id
	 * @return
	 */

@Select("SELECT *"+
		"\t\tFROM\n" +
		"\t\t\tbook\n" +
		"\t\tWHERE\n" +
		"\t\t\tbook_id = #{bookId}")
	Book queryById(int id);

	/**
	 * 查询所有图书
	 * 
	 * @param offset 查询起始位置
	 * @param limit 查询条数
	 * @return
	 */
@Select("SELECT * "+
		"\t\tFROM\n" +
		"\t\t\tbook\n" +
		"\t\tORDER BY\n" +
		"\t\t\tbook_id\n" +
		"\t\tLIMIT #{offset}, #{limit}")//限制大小
	List<Book> queryAll(@Param("offset") int offset, @Param("limit") int limit);

	/**
	 * 减少馆藏数量减少一本
	 *
	 * @param bookId
	 * @return 如果影响行数等于>1，表示更新的记录行数
	 */
	@Update("UPDATE book\n" +
			"\t\tSET number = number - 1\n" +
			"\t\tWHERE\n" +
			"\t\t\tbook_id = #{bookId}\n" +
			"\t\tAND number > 0")
	int reduceNumber(long bookId);

/*/**
* @Description: 管理员操作
* @Param: [bookId]
* @return: [bookId]
* @Author: Miao
* @Date: 2021/12/11
*/
	@Delete("delete from book where book_id =#{bookId}")
	int DeleteBook(int bookId);

	/*/**
	* @Description: 管理员操作
	* @Param: [number, bookId]
	* @return: [number, bookId]
	* @Author: Miao
	* @Date: 2021/12/11
	*/
	@Update("UPDATE book set number = number + #{number} " +
			"where " +
			"book_id = #{bookId}")
	int AddNumber(int number,long bookId);

	/*/**
	* @Description: 插入操作
	* @Param: [bookId, bookName, bookNum]
	* @return: [bookId, bookName, bookNum]
	* @Author: Miao
	* @Date: 2021/12/18
	*/
	@Insert("insert into book values (#{bookId},#{bookName},#{bookNum})")
	int Insert(int bookId,String bookName,int bookNum);

	/*/**
	* @Description: 管理员修改图书
	* @Param:
	* @return:
	* @Author: Miao
	* @Date: 2021/12/21
	*/
	@Update("update book set  name = #{name}, number = #{bookNum}\n" +
			"where book_id = #{bookId};")
	int Update(long bookId,String bookName,int bookNum);

	/*/**
	* @Description: 删除借书记录
	* @Param:
	* @return:
	* @Author: Miao
	* @Date: 2021/12/21
	*/
	@Update("delete from appointment where book_id = #{bookId} and student_id = #{studentId}")
	int deleteAppiont(int bookId,int studentId);

	/**
	 * 减少馆藏数量减少一本
	 *
	 * @param bookId
	 * @return 如果影响行数等于>1，表示更新的记录行数
	 */
	@Update("UPDATE book\n" +
			"\t\tSET number = number + 1\n" +
			"\t\tWHERE\n" +
			"\t\t\tbook_id = #{bookId}\n" +
			"\t\tAND number > 0")
	int AddNumber1(long bookId);

	/*/**
	* @Description: 查询书籍
	* @Param:
	* @return:
	* @Author: Miao
	* @Date: 2021/12/21
	*/
	@Select("SELECT * from book\n" +
			"where name like #{bookName}")
	List<Book> Serach(String bookName);
}
